package myExample;
import org.apache.uima.UimaContext;
import org.apache.uima.analysis_component.CasAnnotator_ImplBase;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.cas.CAS;
import org.apache.uima.cas.CASException;
import org.apache.uima.jcas.JCas;
import org.apache.uima.resource.ResourceAccessException;
import org.apache.uima.resource.ResourceInitializationException;

// analysis engine

public class TownAnnotator extends CasAnnotator_ImplBase {
	private StringMapResources mMap;
	
	public void initialize(UimaContext aContext) throws ResourceInitializationException {
		super.initialize(aContext);
	    // get a reference to the String Map Resource
	    try {
	      mMap = (StringMapResources) getContext().getResourceObject("Town");
	    } catch (ResourceAccessException e) {
	      throw new ResourceInitializationException(e);
	    }
	}
	
	@Override
	public void process(CAS acas) throws AnalysisEngineProcessException {
		// TODO Auto-generated method stub
		JCas jcas = null;
		try {
			jcas = acas.getJCas();
		} catch (CASException e) {
			e.printStackTrace();
		}
		String text = jcas.getDocumentText();
		for (int i=0; i< mMap.getSizeList(); i++) {
			int begin = text.indexOf(mMap.getString(i));
			while (begin >= 0) {
				int end = begin + mMap.getString(i).length();
				Town town = new Town(jcas);
				town.setBegin(begin);
				town.setEnd(end);
				town.setName(mMap.getString(i));
				town.addToIndexes(); // add to cas
				begin = text.indexOf(mMap.getString(i), end);
			}
		}
	}

}
